package com.financing.wap.web.controller.api;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.BizEnums;
import com.financing.wap.common.ResultMap;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.dto.BankInfoDTO;
import com.financing.wap.service.IBankInfoService;
import com.financing.wap.util.MoneyUtil;
import com.financing.wap.vo.BankInfoVO;
import com.financing.wap.web.controller.BaseController;

/**
 * 通用支付模块
 * @author wade
 *
 */
@Controller
@RequestMapping("/api/bank")
public class BankInfoController extends BaseController{
	private Logger logger = Logger.getLogger(getClass());

	@Autowired
	private IBankInfoService bankInfoServiceImpl;
	
	/**
	 * 查询银行参数<br>
	 * 不传bankCode默认返回全部
	 * @param model
	 * @param content
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public ResultMap queryList(ModelMap model, String bankCode,
			HttpServletRequest request, HttpServletResponse response) {
		ResultMap result = new ResultMap();
		
		if( logger.isInfoEnabled() ){
			logger.info("BankInfoController queryList begin!");
		}
		
		try {
			BankInfoDTO bankInfo = new BankInfoDTO();
			if( StringUtils.isNotBlank(bankCode)){
				bankInfo.setBankCode(bankCode);
			}
			
			bankInfo.setValidFlag(BizEnums.BankInfoEnums.BANK_VALID);//只查询有效记录(不支持的银行状态为0)
			bankInfo.setPayPlat(BizEnums.PayEnums.PAY_3PLAT_BAOFOO);//“SFA”虚拟支付商
			
			List<BankInfoDTO> list = bankInfoServiceImpl.queryBankInfoList(bankInfo);

			List<BankInfoVO> voList = toBankInfoVO(list) ;
			
			
			result.setAttribute("list", voList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			if( logger.isInfoEnabled() ){
				logger.info("BankInfoController queryList end!");
			}
		} catch (Exception e) {
		
			logger.error("error BankCardController queryList:", e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}

	private List<BankInfoVO> toBankInfoVO(List<BankInfoDTO> list) {
		
		List<BankInfoVO> vos = new ArrayList<BankInfoVO>() ; 
		for( BankInfoDTO b : list ){
			BankInfoVO vo = new BankInfoVO() ; 
			vo.setBankCode(b.getBankCode());
			vo.setBankName(b.getBankName());
			vo.setBankShortName(b.getBankShortName());
			vo.setDayLimit("单日"+limitDesc(b.getDayLimit()));
			vo.setSingleLimit("单笔"+limitDesc(b.getSingleLimit()));
			vo.setBankLogo(b.getBankLogo());
			vos.add(vo) ;
		}
		
		return vos;
	}

	private String limitDesc(BigDecimal single_limit) {
		if( MoneyUtil.isEqualZero(single_limit) ){
			return "无限额" ;
		}else{
			return String.valueOf(toWanYuan(single_limit)) ;
		}
	}
	
	/**
	 * 把传入的数字转换为万级别
	 * @param product_tot_amt
	 * @return
	 */
	private String toWanYuan(BigDecimal product_tot_amt) {
		//modify by wade 2015-10-09 测试中发现 0.99/10000 发生科学计数法所以只能使用BigDecimal来计算了
		//且使用stripTrailingZeros去掉精度后面多余的0，用toPlainString避免科学计数法显示
		//BigDecimal b1 = new BigDecimal(Double.toString(product_tot_amt));
		BigDecimal b2 = new BigDecimal(Double.toString(10000));
		//转为万除以10000,然后再考虑分100所有需要6为小数
		String result = product_tot_amt.divide(b2, 6, BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString();
		result = result + "万";
		return result;
	}
}
